package com.sh.personnel.mapper;

import com.sh.personnel.domain.ITbUser;
import com.sh.personnel.vo.dto.UserDto;
import org.apache.ibatis.annotations.*;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @author 林思浩
 * @date 2020/02/26 21:54
 */
@Repository
public interface IUserMapper {

    //根据用户名或者邮箱获取用户信息
    @Select("SELECT * FROM tb_user WHERE username = #{username} || email = #{username}")
    ITbUser findUserByUsername(String username);

    //添加用户
    @Insert("INSERT INTO tb_user" +
            " (id, username, password, name, createtime, avatar, email, phone, birthday, district, city, province, departmentid, positionid)" +
            "VALUES" +
            " (#{id}, #{username}, #{password}, #{name}, #{createtime}, #{avatar}, #{email}, #{phone}, #{birthday}, #{district}, #{city}, #{province}, #{departmentid}, #{positionid})")
    int insertUser(ITbUser user);

    //分页获取用户列表
    List<UserDto> findUserList(@Param("key") String key,
                              @Param("deptid") String deptId,
                              @Param("state") Integer state);

    //获取指定用户
    ITbUser findUserById(String id);

    //获取指定用户的角色ID
    @Select("SELECT roleid FROM tb_user_role WHERE userid = #{id}")
    List<Integer> findRoleIdsByUserId(String id);

    //更新指定用户
    @Update("UPDATE tb_user SET name = #{name}, phone = #{phone}, departmentid = #{departmentid}, state = #{state}" +
            " WHERE id = #{id}")
    int updateUserById(ITbUser user);

    //删除指定用户角色
    @Delete("DELETE FROM tb_user_role WHERE userid = #{id}")
    int deleteRoleIdByUserId(@Param("id") String userId);

    //添加指定用户角色
    int insertUserRoleByUserId(@Param("roleids") List<Integer> roleIds,
                               @Param("userid") String userId);

    //删除指定用户
    @Delete("DELETE FROM tb_user WHERE id = #{id}")
    int deleteUserById(String id);
}
